package com.gk.panda.apis.controller;

import com.alibaba.nacos.common.utils.HttpMethod;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.Vo.BiodiversityVo;
import com.gk.panda.apis.Vo.BiologyFileVo;
import com.gk.panda.apis.dto.BiodiversitySaveDto;
import com.gk.panda.apis.dto.BiodiversitySearchDto;
import com.gk.panda.apis.dto.BiologyFileSaveDto;
import com.gk.panda.apis.dto.BiologyFileSearchDto;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.BiodiversityService;
import com.gk.panda.apis.service.BiologyFileService;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.Biodiversity;
import com.gk.panda.pojo.BiologyFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 生物多样性控制器
 */
@RestController
@RequestMapping("/biodiversity")
@Api(tags = "生物多样性")
public class BiodiversityController {

    @Autowired
    private BiodiversityService biodiversityService;

    @Autowired
    private BiologyFileService biologyFileService;

    @ApiOperation(value = "查询生物名录")
    @PostMapping("/page")
    public Result<IPage<BiodiversityVo>> getBiodiversity(@RequestBody BiodiversitySearchDto dto) {
        IPage<BiodiversityVo> biodiversityPage= biodiversityService.getBiodiversity(dto);
        return Result.ok(biodiversityPage);
    }

    @ApiOperation(value = "根据id查询生物详情")
    @GetMapping("/details")
    public Result<Biodiversity> getBiodiversityById(int id) {
        return Result.ok(biodiversityService.getById(id));
    }

    @ApiOperation(value = "添加生物名录")
    @Log
    @PostMapping
    public Result<String> addBiodiversity(@RequestBody BiodiversitySaveDto dto) throws UnsupportedEncodingException {
        int add = biodiversityService.addBiodiversity(dto);
        if (add == -1) return Result.write(201,"添加失败，已有此物种");
        if (add == 0) return Result.write(201,"添加出错");
        return Result.write(200,"添加成功");
    }

    @ApiOperation(value = "修改生物名录")
    @Log
    @PutMapping
    public Result<String> updateBiodiversity(@RequestBody Biodiversity biodiversity) {
        int update = biodiversityService.updateBiodiversity(biodiversity);
        if (update ==-1) return Result.write(201,"没有权限修改");
        if (update == 1) return Result.write(200,"修改成功");
        return Result.write(201,"修改失败");
    }

    @ApiOperation(value = "删除生物名录")
    @Log
    @DeleteMapping("/{id}")
    public Result<String> deleteBiodiversity(@PathVariable("id") int id) {
        int update = biodiversityService.deleteBiodiversity(id);
        if (update == -1) return Result.write(201,"没有权限删除");
        if (update == 1) return Result.write(200,"删除成功");
        return Result.write(201,"删除失败");
    }

    @ApiOperation(value = "查询生物附件")
    @PostMapping("/file/page")
    public Result<Page<BiologyFileVo>> listBiologyFile(@RequestBody BiologyFileSearchDto dto) {
        return Result.ok(biologyFileService.listBiologyFile(dto));
    }

    @ApiOperation(value = "根据id查询生物附件详情")
    @GetMapping("/file/details")
    public Result<BiologyFile> listBiologyFile(int id) {
        return Result.ok(biologyFileService.getById(id));
    }

    @ApiOperation(value = "添加生物附件")
    @Log
    @PostMapping("/file")
    public Result<String> addBiologyFile(@RequestBody BiologyFileSaveDto dto) {
        if (null == dto.getUrl()) return Result.error("文件不能为空！");
        boolean add = biologyFileService.addBiologyFile(dto);
        if (add) return Result.write(200,"添加成功");
        return Result.write(201,"添加失败");
    }

    @ApiOperation(value = "修改生物附件")
    @Log
    @PutMapping("/file")
    public Result<String> updateBiologyFile(@RequestBody BiologyFile biologyFile) {
        boolean update = biologyFileService.updateById(biologyFile);
        if (update) return Result.write(200,"修改成功");
        return Result.write(201,"修改失败");
    }

    @ApiOperation(value = "删除生物附件")
    @Log
    @DeleteMapping("/file/{id}")
    public Result<String> deleteBiologyFile(@PathVariable("id") String id) {
        int update = biologyFileService.deleteBiologyFile(id);
        if (update == -1) return Result.write(201,"没有权限删除");
        if (update == 1) return Result.write(200,"删除成功");
        return Result.write(201,"删除失败");
    }

    @GetMapping("/template")
    @ApiOperation(value = "下载动植物名录导入模板", httpMethod = HttpMethod.GET)
    @ApiImplicitParam(name = "type", value = "下载类型: 0动物类名录，1植物类名录", required = true)
    public void downloadTemplate(HttpServletResponse response, @NotNull @RequestParam("type") Integer type) {
        biodiversityService.downloadTemplate(response, type);
    }

    @PostMapping("/import")
    @Log
    @ApiOperation(value = "批量导入动植物名录信息", httpMethod = HttpMethod.POST)
    @ApiImplicitParam(name = "type", value = "下载类型: 0动物类名录，1植物类名录", required = true)
    public Result<String> importBiology(@RequestPart("file") MultipartFile file, @NotNull @RequestParam("type") Integer type) {
        String result =  biodiversityService.importBiology(file, type);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @GetMapping("/export")
    @Log
    @ApiOperation(value = "批量导出动植物名录信息", httpMethod = HttpMethod.GET)
    @ApiImplicitParam(name = "type", value = "下载类型: 0动物类名录，1植物类名录", required = true)
    public void exportBiology(HttpServletResponse response, @NotNull @RequestParam("type") Integer type) {
        biodiversityService.exportBiology(response, type);
    }

    @ApiOperation(value = "设为封面")
    @Log
    @PutMapping("/file/setCover/{id}")
    public Result<String> setCover(@PathVariable("id") int id) {
        boolean update = biologyFileService.setCover(id);
        if (update) return Result.write(200,"修改成功");
        return Result.write(201,"修改失败");
    }

    @ApiOperation(value = "批量上传生物附件")
    @Log
    @PostMapping("/file/uploadBatch")
    public Result<String> uploadBatch(Integer kingdom, String biologyName, MultipartFile[] files) {
        return Result.ok(biologyFileService.uploadBatch(kingdom,biologyName,files));
    }

    @ApiOperation(value = "查询生物名称列表")
    @PostMapping("/biodiversityNameList")
    public Result<List<Biodiversity>> biodiversityNameList() {
        return Result.ok(biodiversityService.biodiversityNameList());
    }

}
